package com.youu.youu.portal.controller.dispatcher;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.atguigu.util.HttpClientUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youu.youu.manager.bean.TScenicdetails;
import com.youu.youu.portal.bean.ScwReturn;
import com.youu.youu.portal.controller.RestApiServerInfo;

@Controller
@RequestMapping("/scenic")
public class DispatchController {
    
    @Autowired
    RestApiServerInfo apiServerInfo;
    
    @RequestMapping("/list.html")
    public String toScenicsListPage(@RequestParam(value="select_key", defaultValue="null")String selectKey, @RequestParam(value="order", defaultValue="null")String order, @RequestParam(value="no", defaultValue="1")Integer pageNo, Model model) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("select_key", selectKey);
        params.put("order", order);
        params.put("no", pageNo);
        
        String url= apiServerInfo.getRestApiURL()+"/show/scenicdetail/list";
        
        try {
            String getRequest = HttpClientUtil.httpPostRequest(url, params);
            ObjectMapper objectMapper = new ObjectMapper();
            ScwReturn<List<TScenicdetails>> readValue = objectMapper.readValue(getRequest.getBytes(), new TypeReference<ScwReturn<List<TScenicdetails>>>() {});
            if (readValue.getCode() == 1) model.addAttribute("scenics", readValue.getContent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "scenics_list";
    }
}
